const fse = require('fs-extra');
const fs = require('fs');
const path = require('path');

const srcFileNames = String.raw`
├── 1-01.Muddy.Puddles[www.19937.com].srt
├── 1-26.Snow[www.19937.com].mp4
├── 1-01.Muddy.Puddles[www.19937.com].mp4
├── 1-43.Tidying.Up[www.19937.com].mp4
├── 1-50.My.Birthday.Party[www.19937.com].mp4
├── 1-45.Daddy.Puts.Up.a.Picture[www.19937.com].mp4
├── 1-18.Dressing.Up[www.19937.com].mp4
├── 1-15.Picnic[www.19937.com].mp4
├── 1-02.Mr.Dinosaur.Is.Lost[www.19937.com].mp4
├── 1-35.Camping[www.19937.com].mp4
├── 1-38.Fancy.Dress.Party[www.19937.com].mp4
├── 1-29.Pancakes[www.19937.com].mp4
├── 1-03.Best.Friend[www.19937.com].mp4
├── 1-44.The.Playground[www.19937.com].mp4
├── 1-34.Lunch[www.19937.com].mp4
├── 1-27.Windy.Castle[www.19937.com].mp4
├── 1-32.Thunderstorm[www.19937.com].mp4
├── 1-51.Daddy's.Movie.Camera[www.19937.com].mp4
├── 1-12.Bicycles[www.19937.com].mp4
├── 1-24.Treasure.Hunt[www.19937.com].mp4
├── 1-23.The.New.Car[www.19937.com].mp4
├── 1-13.Secrets[www.19937.com].mp4
├── 1-11.Hiccups[www.19937.com].mp4
├── 1-40.Very.Hot.Day[www.19937.com].mp4
├── 1-36.Sleepy.Princess[www.19937.com].mp4
├── 1-37.The.Tree.House[www.19937.com].mp4
├── 1-31.Ballet.Lesson[www.19937.com].mp4
├── 1-16.Musical.Instruments[www.19937.com].mp4
├── 1-21.Mummy.Pig's.Birthday[www.19937.com].mp4
├── 1-41.Chloe's.Puppet.Show[www.19937.com].mp4
├── 1-22.Tooth.Fairy[www.19937.com].mp4
├── 1-47.Mr.Skinny.Legs[www.19937.com].mp4
├── 1-42.Daddy.Gets.Fit[www.19937.com].mp4
├── 1-19.New.Shoes[www.19937.com].mp4
├── 1-39.The.Museum[www.19937.com].mp4
├── 1-04.Polly.Parrot[www.19937.com].mp4
├── 1-10.Gardening[www.19937.com].mp4
├── 1-52.The.School.Play[www.19937.com].mp4
├── 1-14.Flying.a.Kite[www.19937.com].mp4
├── 1-08.The.Piggy.in.the.Middle[www.19937.com].mp4
├── 1-49.Shopping[www.19937.com].mp4
├── 1-28.My.Cousin.Chloe[www.19937.com].mp4
├── 1-05.Hide.and.Seek[www.19937.com].mp4
├── 1-48.Grandpa.Pig's.Boat[www.19937.com].mp4
├── 1-46.At.the.Beach[www.19937.com].mp4
├── 1-25.Not.Very.Well[www.19937.com].mp4
├── 1-33.Cleaning.the.Car[www.19937.com].mp4
├── 1-07.Mummy.Pig.at.Work[www.19937.com].mp4
├── 1-06.The.Playgroup[www.19937.com].mp4
├── 1-20.The.School.Fete[www.19937.com].mp4
├── 1-30.Babysitting[www.19937.com].mp4
├── 1-17.Frogs.and.Worms.and.Butterflies[www.19937.com].mp4
├── 1-09.Daddy.Loses.his.Glasses[www.19937.com].mp4
`;

function getMp4FileName(S, E) {
    for(name of srcFileNames.split('\n')) {
        const [srcS,  srcE] = name.replace('├── ', '').split('.')[0].split('-');
        if (Number(srcS) == Number(S) && Number(srcE) == Number(E)) {
            return name.replace('├── ', '');
        }
    }
    return '';
}

async function copyFile(srcPath, destPath) {
  try {
    // 复制文件
    await fse.copy(srcPath, destPath);
    console.log(`文件已从 ${srcPath} 复制到 ${destPath}`);
  } catch (err) {
    console.error('复制失败：', err.message);
  }
}

// 同步遍历目录函数
async function traverseDirSync(dirPath, depth = 0) {
  try {
    // 读取目录下的所有文件/子目录
    const files = fs.readdirSync(dirPath);
    
    for (const file of files) {
      // 获取文件/目录的完整路径
      const fullPath = path.join(dirPath, file);
      // 获取文件信息（是否为目录）
      const stats = fs.statSync(fullPath);
      
      // 打印层级缩进和文件名
        const [S, E] = file.split('.')[2].replace('S', '').split('E');
        const map4FileName = getMp4FileName(S, E);
        await copyFile(dirPath + '\\' + file, './out/' + map4FileName.replace('.mp4', '.srt'));
      //console.log(`${file}`);
      
      // 如果是目录，递归遍历
      if (stats.isDirectory()) {
        //traverseDirSync(fullPath, depth + 1);
      }
    }
  } catch (err) {
    console.error('遍历错误：', err.message);
  }
}

for(name of srcFileNames.split('\n')) {
    console.log(name.replace('├── ', ''));
}
// 调用示例：遍历当前目录
traverseDirSync(String.raw`D:\BaiduNetdiskDownload\小猪佩奇 1-4季的英文字幕+S01中英双语字幕\Peppa Pig.S01中英双语字幕`);